//
//  WJQTabBarController.m
//  TripTogether
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "WJQTabBarController.h"
#import "WJQCustomButton.h"
#import "WJQNearbyViewController.h"

@interface WJQTabBarController ()

@property (strong, nonatomic) UIVisualEffectView * backgroundview;
@property (strong, nonatomic) UIView * bottomView;
@property (strong, nonatomic) UIImageView * closeImageview;
@property (strong, nonatomic) NSMutableArray * menuviewsArray;
@property (nonatomic,strong) WJQCustomButton *previousBtn;

@end


@implementation WJQTabBarController
//自定义设置tabbar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController
{
    //先删掉自己自定义的
    [self.tabBarImageView removeFromSuperview];
    
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        
    }else
    {
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        
        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [super viewDidLoad];
    self.tabBarImageView = [[UIImageView alloc] init];
    self.tabBarImageView.backgroundColor = [UIColor colorWithWhite:0.898 alpha:1.000];
    self.tabBarImageView.userInteractionEnabled = YES;
    self.tabBarImageView.image = [UIImage imageNamed:@""];
    
    
    [self isHiddenTabBar:NO withViewController:nil];
    
    // 下面的方法是调用自定义的生成按钮的方法
    
    //推荐按钮
    [self creatButtonWithNormalName:@"iconfont-home-2.png" andSelectName:@"iconfont-home.png"andTitle:@"推荐"andIndex:0];
    
    
    //城市猎人按钮
    [self creatButtonWithNormalName:@"iconfont-fenlei-2.png" andSelectName:@"iconfont-fenlei.png" andTitle:@"城市猎人"andIndex:1];
    
    
    //中心按钮
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    centerButton.frame = CGRectMake(0, 0, 41, 41);
    centerButton.backgroundColor = [UIColor whiteColor];
    centerButton.center = CGPointMake(SCREEN_WIDTH / 2, self.tabBarImageView.frame.size.height / 2);
    [centerButton setBackgroundImage:[UIImage imageNamed:@"share_compose_add_photo"] forState:UIControlStateNormal];
    centerButton.adjustsImageWhenHighlighted = NO;
    centerButton.layer.cornerRadius = 20.5;
    centerButton.layer.masksToBounds = YES;
    [centerButton addTarget:self action:@selector(centerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarImageView addSubview:centerButton];
    
    //目的地按钮
    [self creatButtonWithNormalName:@"iconfont-find-2.png"andSelectName:@"iconfont-find.png"andTitle:@"目的地"andIndex:2];
    
    //我的按钮
    [self creatButtonWithNormalName:@"iconfont-yonghu-2.png"andSelectName:@"iconfont-yonghu.png"andTitle:@"我"andIndex:3];
    
    //默认选中第一个按钮
    WJQCustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nightAction:) name:@"night" object:nil];
}
- (void)nightAction:(NSNotification *)notification {
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.tabBarImageView.backgroundColor = notification.object;
    }
    else {
        
        self.tabBarImageView.backgroundColor = [UIColor colorWithWhite:0.898 alpha:1.000];
    }
    
}


#pragma mark 创建按钮通用方法
- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index
{
    WJQCustomButton *button = [WJQCustomButton buttonWithType:UIButtonTypeCustom ];
    button.adjustsImageWhenHighlighted = NO;
    button. tag = index;
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    button.frame = CGRectMake (0, 0, buttonW, buttonH);
  
    if (index < 2) {
        
        button.center = CGPointMake((SCREEN_WIDTH / 10) + (SCREEN_WIDTH / 5 * index), self.tabBarImageView.frame.size.height / 2);
        
    } else {
        
        button.center = CGPointMake((SCREEN_WIDTH / 5) + (SCREEN_WIDTH / 10) + (SCREEN_WIDTH / 5 * index), self.tabBarImageView.frame.size.height / 2);
        
    }
    

    
    [button setImage :[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [button setImage :[[UIImage imageNamed:selected] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState : UIControlStateDisabled];//设置按钮无效时的图片样式
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1] forState:UIControlStateDisabled];
    button.tintColor = [UIColor colorWithRed:0.79 green:0.72 blue:0.58 alpha:1];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];
    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    button.titleLabel.font = [ UIFont systemFontOfSize:11]; // 设置标题的字体大小
    [self.tabBarImageView addSubview:button];
    
}

#pragma mark 按钮被点击时调用
- ( void )changeViewController:( WJQCustomButton *)sender
{
    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if ( self.previousBtn != sender) {
        
        self.previousBtn.enabled = YES ;
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}

#pragma mark 中间按钮的点击事件
- (void)centerAction:(UIButton *)button {
    
    UIVisualEffectView * blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    self.backgroundview = blurView;
    [blurView setFrame:self.view.frame];
    [self.view addSubview:blurView];
    [self showMenus];
}

#pragma mark 动画
-(void)showMenus{
    
    NSArray * imagesNameArray = @[@"ic_poi_scenery", @"ic_poi_hotel", @"ic_poi_eat", @"ic_poi_entertainment", @"ic_poi_shopping", @"ic_poi_others"];
    NSArray * labelsArray = @[@"景点", @"住宿", @"餐厅", @"休闲娱乐", @"购物", @"全部"];
    CGFloat width = self.view.frame.size.width;
    CGFloat itemWidth = width / 3;
    self.menuviewsArray = [NSMutableArray new];
    for (int index  = 0;index < 6;index ++) {
        NSTimeInterval delay = 0.03 * index;
        CGFloat centerx;
        CGFloat centery;
        
        if (index < 3) {
            centerx = width /6 * (2*index + 1);
            centery = self.view.frame.size.height - itemWidth - itemWidth - itemWidth/2 - 49;
        }else{
            centerx = width/6 * (2 *(index - 2) -1);
            centery = self.view.frame.size.height - itemWidth - itemWidth/2 - 49;
        }
        //        centerx = width / 2;
        //        centery = self.view.frame.size.height - itemWidth - 100 * index;
        //
        UIImage * image  = [UIImage imageNamed:imagesNameArray[index]];
        NSString * title = labelsArray[index];
        UIView * containview = [self createOneMenuViewWithImage:image Title:title];
        containview.center = CGPointMake(centerx, self.view.frame.size.height);
        UITapGestureRecognizer * menuTab = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(catchMenuTap:)];
        [containview addGestureRecognizer:menuTab];
        [self.view addSubview:containview];
        [self.menuviewsArray addObject:containview];
        containview.tag = index;
        
        [UIView animateWithDuration:0.3 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
            containview.center = CGPointMake(centerx, centery - 30);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                containview.center = CGPointMake(centerx, centery);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
    UIView * bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49)];
    //颜色
    bottomView.backgroundColor = [UIColor colorWithWhite:0.875 alpha:1.000];
    
    
    UIImageView * closeImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 41, 41)];
    [bottomView addSubview:closeImageview];
    closeImageview.backgroundColor = [UIColor whiteColor];
    closeImageview.layer.cornerRadius = 20.5;
    closeImageview.layer.masksToBounds = YES;
    //图片
    closeImageview.image = [UIImage imageNamed:@"share_compose_add_photo"];
    closeImageview.center = CGPointMake(CGRectGetWidth(bottomView.bounds) / 2, CGRectGetHeight(bottomView.bounds) / 2 - 0.5);
    self.closeImageview = closeImageview;
    
    
    //添加手势
    UITapGestureRecognizer * closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenus)];
    closeImageview.userInteractionEnabled = true;
    [closeImageview addGestureRecognizer:closeTap];
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    [UIView animateWithDuration:0.4 animations:^{
        closeImageview.transform = CGAffineTransformMakeRotation(M_PI / 4);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark 隐藏菜单栏
-(void)hideMenus{
    
    [UIView animateWithDuration:0.4 animations:^{
        self.closeImageview.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self.bottomView removeFromSuperview];
        [self.backgroundview removeFromSuperview];
    }];
    for (NSInteger index = 5; index >= 0; index--) {
        UIView * containview = self.menuviewsArray[index];
        CGFloat  dealy = (5 - index) * 0.03;
        [UIView animateWithDuration:0.2 delay:dealy options:UIViewAnimationOptionCurveLinear animations:^{
            containview.center = CGPointMake(containview.center.x, self.view.frame.size.height + CGRectGetWidth(containview.frame)/2);
        } completion:^(BOOL finished) {
            [containview removeFromSuperview];
        }];
    }
    self.menuviewsArray = nil;
}

-(UIView *)createOneMenuViewWithImage:(UIImage *)image Title:(NSString *)title{
    CGFloat width = self.view.frame.size.width;
    CGFloat itemWidth = width / 5;
    UIView * containview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, itemWidth)];
    UIImageView * iconImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, itemWidth - 10, itemWidth - 10)];
    
    iconImageview.center = CGPointMake(itemWidth/2, itemWidth/2);
    iconImageview.contentMode = UIViewContentModeScaleAspectFill;
    iconImageview.image = image;
    [containview addSubview:iconImageview];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, itemWidth, itemWidth, 20)];
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    [containview addSubview:label];
    
    return containview;
}

-(void)catchMenuTap:(UITapGestureRecognizer *)tap{
    

    WJQNearbyViewController *nearVC = [WJQNearbyViewController new];
    
    if (5 == tap.view.tag) {
        nearVC.initNum = 0;
    } else {
        nearVC.initNum = tap.view.tag + 1;
    }
    
    UINavigationController *navigationC = [[UINavigationController alloc] initWithRootViewController:nearVC];
    [self presentViewController:navigationC animated:YES completion:^{
        
        
    }];
}

@end
